﻿using AutoMapper;
using HikRcsService.Models;
using Newtonsoft.Json;
using Serilog;
using WCS_Core_Configuration;
using WCS_Service.DomianService.Models;
using WCS_Service.RcsServices.Interfaces;

namespace WCS_Service.RcsServices
{
    /// <summary>
    /// 创建海康RCS所需的参数
    /// </summary>
    public class HikParabuildService : IHikParabuildService
    {
        private readonly IMapper _mapper;
        public HikParabuildService(IMapper mapper) 
        {
            _mapper = mapper;
        }

        public (string, string) CreatBindCtnrAndBin(object para)
        {
            var info = para as BindCtnrAndBinModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BindCtnrAndBinPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BindCtnrAndBinModel)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatbindMaterialTypCodeAndBerth(object para)
        {
            var info = para as BindMaterialTypCodeAndBerthModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BindMaterialTypCodeAndBerthPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BindMaterialTypCodeAndBerthModel)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatBindPodAndBerth(object para)
        {
            var info = para as BindPodAndBerthModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BindPodAndBerthPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BindPodAndBerthPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatBindPodAndMat(object para)
        {
            var info = para as BindPodAndMatModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BindPodAndMatPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BindPodAndMatPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatBlockArea(object para)
        {
            var info = para as BlockAreaModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BlockAreaPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BlockAreaPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatBlockStgBin(object para)
        {
            var info = para as BlockStgBinModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BlockStgBinPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BlockStgBinPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatBoxApplyPass(object para)
        {
            var info = para as BoxApplyPassModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<BoxApplyPassPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(BoxApplyPassPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatCancelTask(object para)
        {
            var info = para as CancelTaskModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<CancelTaskPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(CancelTaskPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatClearRoadWay(object para)
        {
            var info = para as ClearRoadWayModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<ClearRoadWayPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(ClearRoadWayPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatContinueTask(object para)
        {
            var info = para as ContinueTaskModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<ContinueTaskPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(ContinueTaskPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatTask(object para)
        {
            var info = para as CreatTaskModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<CreatTaskPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(CreatTaskPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatGenCtuGroupTaskBatch(object para)
        {
            var info = para as GenCtuGroupTaskBatchModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<GenCtuGroupTaskBatchPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(GenCtuGroupTaskBatchPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatGetOutPod(object para)
        {
            var info = para as GetOutPodModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<GetOutPodPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(GetOutPodPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatLockPosition(object para)
        {
            var info = para as LockPositionModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<LockPositionPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(LockPositionPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatQueryAgv(object para)
        {
            var info = para as QueryAgvModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<QueryAgvPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(QueryAgvPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatQueryPodBerthAndMat(object para)
        {
            var info = para as QueryPodBerthAndMatModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<QueryPodBerthAndMatPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(QueryPodBerthAndMatPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatQueryTaskStatus(object para)
        {
            var info = para as QueryTaskStatusModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<QueryTaskStatusPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(QueryTaskStatusPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatRecoveryAgv(object para)
        {
            var info = para as RecoveryAgvModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<RecoveryAgvPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(RecoveryAgvPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatSetPriority(object para)
        {
            var info = para as SetPriorityModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<SetPriorityPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(SetPriorityPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatStopAgv(object para)
        {
            var info = para as StopAgvModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<StopAgvPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(StopAgvPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }
        public (string, string) CreatSyncMapDatas(object para)
        {
            var info = para as SyncMapDatasModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<SyncMapDatasPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(SyncMapDatasPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatReturnPod(object para)
        {
            var info = para as ReturnPodModel;
            if (info is null)
            {
                return (string.Empty,string.Empty);
            }
            var result = _mapper.Map<ReturnPodPara>(info);
            if (result is null)
            {
                Log.Error($"Map数据(ReturnPodPara)转换失败!");
                return (string.Empty,string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        public (string, string) CreatPreScheduleTask(object para)
        {
            var info = para as GenPreScheduleTaskModel;
            if (info is null)
            {
                return (string.Empty, string.Empty);
            }
            var result = _mapper.Map<GenPreScheduleTaskPara>(info);
            if (result is null)
            {
                Log.Error($"预调度数据(GenPreScheduleTaskPara)转换失败!");
                return (string.Empty, string.Empty);
            }
            result.reqCode = GetreqCode();
            result.reqTime = GetTimeStamp();
            result.clientCode = AppSettingsConstVars.WcsClientCode;
            result.tokenCode = AppSettingsConstVars.RcsToken;
            return (JsonConvert.SerializeObject(result), result.reqCode);
        }

        private string GetreqCode ()=> Guid.NewGuid().ToByteArray().Aggregate<byte, long>(1, (current, b) => current * (b + 1)).ToString();

        private string GetTimeStamp () => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    }
}
